<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Parameters extends Auth_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('parameters_mdl');
        $this->load->model('group_mdl');
        $this->load->model('prices_mdl');
    }

    public function index(){
        if($_SESSION['user']['status'] == 3){
            show_error('没有权限！');
        }
        $pid = $this->input->get('pid');
        $data['pid'] = $pid = intval($pid);

        if(empty($pid)){
            show_error('参数错误！');
        }

        $this->db->where('project_id',$pid);
        $this->db->where('status',1);
        $this->db->order_by('sort desc');
        $data['list'] = $this->parameters_mdl->select();

        //查询所有权限
        $this->db->order_by('condition desc');
        $data['list_group'] = $this->group_mdl->select();

        //查询对应的价格表
        $list_price = array();
        foreach($data['list'] as $key => $value){
            $this->db->where('par_id',$value['id']);
            $list = $this->prices_mdl->select();

            foreach($list as $k => $v){
                $list_price[$value['id']][$v['group_id']] = $v;
            }
        }
        $data['list_price'] = $list_price;

        $this->_view('index',$data);
    }

    /**
     * 项目详情
     */
    public function show(){
        $pid = $this->input->get('pid');
        $pid = intval($pid);

        $this->db->where('id',$pid);
        $this->db->where('status',1);
        $res = $this->parameters_mdl->get_one();

        if(empty($res)){
            show_error('该项目不存在，或者已经被下架。');
        }

        //查询文章
        $this->load->model('article_mdl');
        $this->db->where('id',$res['article_id']);
        $article = $this->article_mdl->get_one();

        //查询项目属性
        $this->db->where('project_id',$res['project_id']);
        $this->db->where('status',1);
        $this->db->select('id,title');
        $this->db->order_by('sort desc');
        $pro_list = $this->parameters_mdl->select();
        $data = array(
            'res'=>$res,
            'article'=>$article,
            'list'=>$pro_list,
        );

        //管理员用户查看各个权限详细价格
        if($_SESSION['group'] != 3){
            $this->db->order_by('condition desc');
            $data['groups_list'] = $this->group_mdl->select();
            $this->load->model('prices_mdl');

            //价格组装 [项目属性ID][权限ID][价格]
            $this->db->where('par_id',$pid);
            $prices_list = $this->prices_mdl->select();
            $data['prices_list'] = array();
            foreach($prices_list as $k => $v){
                $data['prices_list'][$v['group_id']] = $v['price'];
            }

        }

        /**
         * 注意： 该页面显示的是默认价格， 具体的结算需要去订单表
         */
        $this->_view('show',$data);
    }

    /**
     * 排序
     */
    public function set_sort(){
        if($_SESSION['user']['status'] !== 3){
            show_error('没有权限！');
        }
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }

        $post = $this->input->post();

        $this->db->where('id',intval($post['pid']));
        $res = $this->parameters_mdl->update(array('sort'=>intval($post['sort'])));
        echo $res === TRUE ? 1 : '操作失败';
    }

    /**
     * 属性删除
     */
    public function delete(){
        if($_SESSION['user']['status'] == 3){
            show_error('没有权限！');
        }
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }

        $pid = $this->input->post('pid');
        $pid = intval($pid);

        $this->db->where('id',$pid);
        $res = $this->parameters_mdl->update(array('status'=>2));
        echo $res === true ? 1 : '操作失败！';
    }

    /**
     * 新增属性
     */
    public function create(){
        if($_SESSION['user']['status'] == 3){
            show_error('没有权限！');
        }
        if($this->input->is_ajax_request()){
            $post = $this->input->post();

            if(empty($post['title'])){
                die('属性名称不能为空');
            }
            if(intval($post['article_id']) < 1){
                die('请选择属性文档');
            }
            if(intval($post['pid']) < 1){
                die('参数错误！');
            }

            if(!is_numeric($post['d_price']) || $post['d_price'] < 1){
                die('请正确填写默认价格');
            }

            foreach($post['price'] as $k => $v){
                if($v < 1 || !is_numeric($v)){
                    die('请填写每个权限的价格');
                }
            }

            //添加进项目属性
            $data = array(
                'project_id' => intval($post['pid']),
                'title' => $post['title'],
                'article_id' => $post['article_id'],
                'price' => $post['d_price'],
                'update_time' => time(),
                'sort' => 0,
                'status' => 1
            );

            $this->db->trans_start();
            $pro_id = $this->parameters_mdl->create($data);

            //循环添加进价格
            foreach($post['price'] as $k => $v){
                $val_add = array(
                    'par_id' => $pro_id,
                    'group_id' => $k,
                    'price' => $v,
                    'sort' => 0
                );
                $re = $this->prices_mdl->create($val_add);

                if(!$re){
                    die('添加失败！');
                }
            }
            $this->db->trans_complete();

            echo 1;
        }else{
            $pid = $this->input->get('pid');
            $pid = intval($pid);

            if($pid <= 0){
                show_error('参数错误！');
            }

            //查询所有权限表
            $this->db->order_by('condition asc');
            $list_group = $this->group_mdl->select();

            $this->_view('create',array('pid'=>$pid,'list_group' => $list_group));
        }
    }

    /**
     * 更改属性
     */
    public function update(){
        if($_SESSION['user']['status'] == 3){
            show_error('没有权限！');
        }
        if($this->input->is_ajax_request()){
            $post = $this->input->post();
            if(empty($post['title'])){
                die('属性名称不能为空');
            }
            if(intval($post['article_id']) < 1){
                die('请选择属性文档');
            }
            if(intval($post['pid']) < 1){
                die('参数错误！');
            }

            if(!is_numeric($post['d_price']) || $post['d_price'] < 1){
                die('请正确填写默认价格');
            }

            foreach($post['price'] as $k => $v){
                if($v < 1 || !is_numeric($v)){
                    die('请填写每个权限的价格');
                }
            }

            $data = array(
                'title' => $post['title'],
                'article_id' => $post['article_id'],
                'price' => $post['d_price'],
                'update_time' => time(),
                'status' => 1
            );

            $this->db->trans_start();
            $this->db->where('id',intval($post['pid']));
            $this->parameters_mdl->update($data);

            //修改进项目价格

            foreach($post['price'] as $k => $v){
                $this->db->start_cache();
                $this->db->where('par_id',$post['pid']);
                $this->db->where('group_id',$k);
                $this->db->stop_cache();

                //判断价格是否存在
                $res = $this->prices_mdl->get_one();
                if(empty($res)){
                    $this->db->flush_cache();
                    $data = array(
                        'par_id' => $post['pid'],
                        'group_id' => $k,
                        'price' => $v,
                        'sort' => '0'
                        );
                    $re = $this->prices_mdl->create($data);

                }else{
                    $this->db->set('price',$v);
                    $re = $this->prices_mdl->update();
                    $this->db->flush_cache();
                }

                if(!$re){
                    die('操作失败！');
                }

            }

            $this->db->trans_complete();

            echo 1;


        }else{
            //pid 为属性表ID
            $pid = $this->input->get('pid');
            $pid = intval($pid);

            $this->db->where('id',$pid);
            $this->db->where('status',1);
            $res = $this->parameters_mdl->get_one();

            if(empty($res)){
                show_error('参数错误！');
            }

            //查询文章标题
            $this->load->model('article_mdl');
            $this->db->where('id',$res['article_id']);
            $this->db->select('title');
            $res_art = $this->article_mdl->get_one();
            $res['article_title'] = $res_art['title'];

            //查询所有权限
            $this->db->order_by('condition asc');
            $list_group = $this->group_mdl->select();

            //查询相关权限价格
            $this->db->where('par_id',$pid);
            $list = $this->prices_mdl->select();

            $list_price = array();
            foreach($list as $k => $v){
                $list_price[$v['group_id']] = $v['price'];
            }


            $this->_view('update',array(
                'res'=>$res,
                'list_group' => $list_group,
                'list_price' => $list_price
                )
            );
        }
    }

}

/* End of file Parameters.php */
/* Location: ./application/models/Parameters.php */